/**
 * 
 */
package com.kelaite.procurement.restful;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import ch.qos.logback.classic.Logger;

import com.kelaite.procurement.common.CommonResponseDto;
import com.kelaite.procurement.common.RetCodeEnum;
import com.kelaite.procurement.dto.UserDTO;
import com.kelaite.procurement.entity.TIndustry;
import com.kelaite.procurement.entity.TUser;
import com.kelaite.procurement.entity.TVendor;
import com.kelaite.procurement.service.CompanyService;
import com.kelaite.procurement.service.IndustryService;
import com.kelaite.procurement.service.UserService;
import com.kelaite.procurement.service.VendorService;
import com.kelaite.procurement.utils.CurrentUserUtils;
import com.kelaite.procurement.utils.JsonUtil;

@Controller
@RequestMapping("/api")
public class IndustryRestController {

	@SuppressWarnings("unused")
	private static final Logger logger = (Logger) LoggerFactory
			.getLogger(IndustryRestController.class);

	@Autowired
	private IndustryService industryService;

	@Autowired
	private CompanyService companyService;

	@Autowired
	private VendorService vendorService;

	@Autowired
	private UserService userService;

	@ResponseBody
	@RequestMapping(value = "/{industryId}/industry", method = { RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_VALUE)
	public String changeIndustry(@PathVariable("industryId") long industryId) {

		UserDTO userDTO = CurrentUserUtils.getInstance().getUser();
		TUser currentUser = this.userService.getObject(userDTO.getId());
		List<TVendor> vendorList = this.vendorService.getByProperty("tClient",
				currentUser.gettCompany());
		Set<TVendor> targetVendorList = new HashSet<TVendor>();
		if (industryId == 0) {
			for (TVendor vendor : vendorList) {
				if (vendor.gettSupplier().getMainBusiness() != null) {
					boolean flag = false;
					for (TVendor tempVendor : targetVendorList) {
						if (tempVendor.gettSupplier().getId() == vendor
								.gettSupplier().getId()) {
							flag = true;
							break;
						}
					}
					if (!flag) {
						targetVendorList.add(vendor);
					}
				}
			}
			return JsonUtil.serialize(new CommonResponseDto<Set<TVendor>>(
					RetCodeEnum.SUCCESS.getCode(),
					RetCodeEnum.SUCCESS.getMsg(), targetVendorList));
		} else {
			TIndustry industry = this.industryService.getObject(industryId);
			List<TIndustry> industryList = new ArrayList<TIndustry>();
			industryList.add(industry);
			for (TIndustry industryObj : industryList) {
				for (TVendor vendor : vendorList) {
					if (vendor.gettSupplier().getMainBusiness() != null
							&& vendor
									.gettSupplier()
									.getMainBusiness()
									.contains(
											String.valueOf(industryObj.getId()))) {
						boolean flag = false;
						for (TVendor tempVendor : targetVendorList) {
							if (tempVendor.gettSupplier().getId() == vendor
									.gettSupplier().getId()) {
								flag = true;
								break;
							}
						}
						if (!flag) {
							targetVendorList.add(vendor);
						}
					}
				}
			}
			return JsonUtil.serialize(new CommonResponseDto<Set<TVendor>>(
					RetCodeEnum.SUCCESS.getCode(),
					RetCodeEnum.SUCCESS.getMsg(), targetVendorList));
		}
	}
}
